<?

/*
 * Menu TEMPLATE (meta sensitive & with subs)
 * 
 * renders a menu extended meta values.
 * 
 * Paramaters:
 * -	Extra value
 *
 */

global $Engine, $DB, $Page, $Redir;

	//  Check if page is object redirect
	$Redir	= $Engine->map->traceRealPage();
	define( "CURRENT", ( $Redir )? $Redir->get( "name" ) : PAGE );
	
	
	foreach( $_SESSION['map']['menu'] as $node ) {
		
		$Node = new PAGE(( int ) $node[ 'ID' ]);	
		$menu[] = render_item( $Node );
	}


	function render_item( $Node ){
		global $Page, $Redir;

		if( $Node->visible() && ( !$Redir || $Node->get( "name" ) != PAGE )) {
			$active = ( $Node->get( "name" ) == CURRENT && $Node->get( "active" ) == "1" )? true : (( !$Redir )? ( $Node->id == $Page->get( 'parent' )) : (( $Node->id == $Redir->get( 'parent' ))? true : false )); 
			
			return "<li" . ( $active? " class='active'": null ) . "><a href='{$Node->meta_uri()}'>{$Node->get( "title" )}</a>" . render_sub( $Node ) . "</li>";
		}
	}
	
	function render_sub( $Node ) {
		if( $subs = $Node->get( 'sub' )) {
			foreach( explode( ",", $subs ) as $sub ) {
				
				$Sub		= new PAGE(( int ) $sub );
				$items[]	= render_item( $Sub ); 
			}
			
			return "<ul class='sub'>" . implode( "\n", $items ) . "</ul>";
		}	
	}


?>

<ul id='menu'><?= implode( "\n", $menu ) ?></ul>